package com.link.base.base.appduty.controller;

import com.link.base.enterprise.model.CoreEnterprise;
import com.link.base.base.appduty.model.AppMenu;
import com.link.base.base.appduty.service.AppMenuService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.interceptor.SecurityInterceptor;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.Filter;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author unknwon
 * @version 1.0
 * @date: 2018/11/8 13:59
 */

@Controller
@RequestMapping("/link/appMenu")
public class AppMenuController extends BasicController<AppMenu> {

    @Resource
    private AppMenuService appMenuService;

    @Override
    public BasicService<AppMenu> getBasicService() throws Exception {
        if (appMenuService != null) {
            return appMenuService;
        }
        throw new ServiceException("PUBLIC-005");
    }

    /**
     * 重写分页查询方法，用于控制个性化菜单中的行业版本限制
     *
     * @author ouyangzq 2018/3/28
     */
    @Override
    @RequestMapping(value = "/queryByExamplePage")
    @ResponseBody
    public Map<String, Object> queryByExamplePage(@JsonParam QueryParams qps, @JsonParam AppMenu record, HttpSession session,
                                                  HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            /** 获取行业版本信息 */
            LCLogger.info().withMessageKey("queryByExamplePage").withMessage("获取行业版本信息...").flush();
            CoreEnterprise enterprise = SecurityInterceptor.tempEnterpirse.get();
            String industryVersion = enterprise.getIndustryVersion();
            if (StringUtils.isBlank(industryVersion)) {
                throw new ServiceException("APPDUTY-001");
            }
            AppMenu t = (AppMenu) BasicModel.transformClass(AppMenu.class, qps);
            Field f = t.getClass().getDeclaredField(industryVersion);
            f.setAccessible(true);
            f.set(t, "Y");

            List<AppMenu> list = getBasicService().queryByExamplePage(t);
            result.put("success", true);
            result.put("rows", list);
            this.pushCurrentDataCache(list, session.getId(), "base", false);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (BasicServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<AppMenu>());
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getDetailMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<AppMenu>());
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping("/unSelectMenuList")
    @ResponseBody
    public Map<String, Object> test(@JsonParam QueryParams qps, @JsonParam AppMenu appMenu, HttpSession session) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            CoreEnterprise enterprise = SecurityInterceptor.tempEnterpirse.get();
            String industryVersion = enterprise.getIndustryVersion();
            if (StringUtils.isBlank(industryVersion)) {
                throw new ServiceException("APPDUTY-001");
            }
            /* 注意，industryVersion(企业表里的行业版本字段的值)与菜单表相应的行业字段必须一一对应，对应关系请
             * 对照Menu对象的相应字段和值列表中类型为INDUSTRY_VERSION的记录的独立源代码
             */
            qps.addFilter(new Filter(industryVersion, "Y"));
            AppMenu menuParam = (AppMenu) AppMenu.transformClass(AppMenu.class, qps);
            List<AppMenu> list = appMenuService.queryUnSelectMenuListPage(menuParam);
            result.put("rows", list);
            result.put("total", getCount(menuParam, list.size()));
            result.put("success", true);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 获取所有菜单及安全性，用于app菜单快速设置
     *
     * @author 李莹
     * @date 2020/07/16 05:10
     */
    @PostMapping(value = "/getAllMenuList", name = "获取所有菜单及安全性")
    @ResponseBody
    public Map<String, Object> getAllMenuList(@JsonParam AppMenu menu, HttpSession session,
                                              HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> result = new HashMap<>(8);
        try {
            result = appMenuService.getAllMenuList(menu);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }
}
